package com.qf.dao.impl;

import com.qf.dao.AccountDao;
import com.qf.pojo.Account;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.sql.SQLException;
import java.util.List;

/**
 * @ClassName a
 * @Description TODO
 * @Author Windy
 * @Date 2022/8/31 10:10
 * @Version 1.0
 **/
public class AccountDaoImpl implements AccountDao {

    private JdbcTemplate jdbcTemplate;

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    /**
     * 根据id查找账户信息
     * @param name
     * @return
     */
    public Account findAccountByName(String name) {
        List<Account> accounts = jdbcTemplate.query("select * from account where name = ?", new BeanPropertyRowMapper<Account>(Account.class), name);
        return accounts.get(0);
    }

    /**
     * 修改账户信息
     * @param account
     */
    public void updateAccount(Account account) {
        String sql = "update account set money=? where name=?";
        try {
            jdbcTemplate.update(sql,account.getMoney(),account.getName());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
